﻿using Microsoft.VisualBasic;
using System.Text.Json;

using XServer.Managers;
using XServer.Models.DrawCards;

namespace XServer.Systems
{
    /// <summary>
    /// 抽卡系统
    /// </summary>
    public class DrawCardsSystem
    {
        public IDictionary<string, IDictionary<CardPoolType, DrawCardsManager>> _userCardPools = new Dictionary<string, IDictionary<CardPoolType, DrawCardsManager>>();

        //public IDictionary<CardPoolType, DrawCardsManager> _drawCardsManagers = new Dictionary<CardPoolType, DrawCardsManager>();

        public IDictionary<CardPoolType, DrawCardsManager> InitCardPools()
        {
            string content = File.ReadAllText("./configs/cardpool.json");
            CardPoolConfig[]? configs = JsonSerializer.Deserialize<CardPoolConfig[]>(content);
            var drawCardsManagers = new Dictionary<CardPoolType, DrawCardsManager>();

            if (configs == null)
            {
                return drawCardsManagers;
            }

            foreach (var config in configs)
            {
                var cardPool = new CardPool(config);
                var manager = new DrawCardsManager(cardPool);
                drawCardsManagers.Add(config.cardPoolType, manager);
            }
            return drawCardsManagers;
        }

        public void RegistUserCardPool(string uid)
        {
            if (_userCardPools.ContainsKey(uid))
            {
                return;
            }
            var drawCardsManagers = InitCardPools();
            _userCardPools.Add(uid, drawCardsManagers);
        }

        public List<Card>? SinglePull(CardPoolType cardPool, out int isup, string uid)
        {
            return ProcessPulls(cardPool, 1, out isup, uid);
        }

        public List<Card>? TenPulls(CardPoolType cardPoolType, out int isup, string uid)
        {
            return ProcessPulls(cardPoolType, 10, out isup, uid);
        }

        public List<Card>? ProcessPulls(CardPoolType cardPoolType, int count, out int isup, string uid)
        {
            RegistUserCardPool(uid);
            var drawCardsManagers = _userCardPools[uid];
            isup = 0;

            if (!drawCardsManagers.ContainsKey(cardPoolType))
            {
                return null;
            }

            var manager = drawCardsManagers[cardPoolType];
            List<Card> cards = new List<Card>();
            int fourCount = 0;

            for (int i = 0; i < count; i++)
            {
                var card = manager.GetCachaResult(fourCount);
                if (card.cardType == CardType.SR)
                {
                    fourCount++;
                }
                if (card.cardType == CardType.SSR)
                {
                    isup++;
                }
                if (card.isUp)
                {
                    isup++;
                }
                cards.Add(card);
            }

            return cards;
        }

        public CardPool? GetCardPoolInfos(string uid, CardPoolType cardPoolType)
        {
            RegistUserCardPool(uid);
            var drawCardsManagers = _userCardPools[uid];

            if (!drawCardsManagers.ContainsKey(cardPoolType))
            {
                return null;
            }

            return drawCardsManagers[cardPoolType]._cardPool;
        }

    }
}
